/*
 * Metaverse Core
 * Copyright (C) 2010 Vyacheslav Vorobyov, All rights reserved.
 * See LICENSE for full license information.
 */
package org.metaverse.warehouse.com.util;

import java.util.LinkedHashMap;
import java.util.Map;

import org.metaverse.warehouse.Senary.Slot;
import org.metaverse.warehouse.com.Constraint;
import org.metaverse.warehouse.com.Equals;
import org.metaverse.warehouse.com.ValueConstraint;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;

/**
 * @author vjache
 *
 */
public class BasicConstraint implements Constraint {

	private ValueConstraint _subj;
	private ValueConstraint _pred;
	private ValueConstraint _obj;
	private ValueConstraint _ctx;
	private ValueConstraint _user;
	private ValueConstraint _lat;

	/**
	 * 
	 */
	public BasicConstraint() {}
	
	public BasicConstraint bySubject(Resource aSubj)
	{
		if(aSubj!=null)
			_subj = new Equals(aSubj);
		return this;
	}
	public BasicConstraint byPredicate(URI aPred)
	{
		if(aPred!=null)
			_pred = new Equals(aPred);
		return this;
	}
	public BasicConstraint byObject(Value aObj)
	{
		if(aObj!=null)
			_obj = new Equals(aObj);
		return this;
	}
	public BasicConstraint byContext(Resource aContext)
	{
		if(aContext!=null)
			_ctx = new Equals(aContext);
		return this;
	}
	public BasicConstraint byUser(URI aUser)
	{
		if(aUser!=null)
			_user = new Equals(aUser);
		return this;
	}
	public BasicConstraint byLastAssertTime(ValueConstraint aLatConstr)
	{
		if(aLatConstr!=null)
			_lat = aLatConstr;
		return this;
	}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getContext()
	 */
	@Override
	public ValueConstraint getContext() 
	{return _ctx;}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getLastAssertTime()
	 */
	@Override
	public ValueConstraint getLastAssertTime() 
	{return _lat;}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getObject()
	 */
	@Override
	public ValueConstraint getObject() 
	{return _obj;}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getPredicate()
	 */
	@Override
	public ValueConstraint getPredicate() 
	{return _pred;}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getSubject()
	 */
	@Override
	public ValueConstraint getSubject() 
	{return _subj;}

	/* (non-Javadoc)
	 * @see org.metaverse.warehouse.com.Constraint#getUser()
	 */
	@Override
	public ValueConstraint getUser() 
	{return _user;}

	@Override
	public Map<Slot, ValueConstraint> getAll() {
		LinkedHashMap<Slot, ValueConstraint> map = new LinkedHashMap<Slot, ValueConstraint>(6);
		map.put(Slot.Subject, _subj);
		map.put(Slot.Predicate, _pred);
		map.put(Slot.Object, _obj);
		map.put(Slot.Context, _ctx);
		map.put(Slot.User, _user);
		map.put(Slot.LastAssertTime, _lat);
		return map;
	}

	public static BasicConstraint constraint(Value...nodes)
	{
		final BasicConstraint c = new BasicConstraint();
		for(int i=0;i<nodes.length;i++)
		{
			switch(i)
			{
			case 0:c.bySubject((Resource) nodes[i]);break;
			case 1:c.byPredicate((URI) nodes[i]);break;
			case 2:c.byObject(nodes[i]);break;
			case 3:c.byContext((Resource) nodes[i]);break;
			case 4:c.byUser((URI) nodes[i]);break;
			default:
				throw new ArrayIndexOutOfBoundsException(i);
			}
		}
		return c;
	}
	
	@Override
	public String toString() {
		return getAll().toString();
	}
}
